package com.wyp168.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyp168.servicebase.exceptionhandler.GuliException;
import com.wyp168.serviceedu.entity.EduChapter;
import com.wyp168.serviceedu.entity.EduVideo;
import com.wyp168.serviceedu.entity.chapter.ChapterVo;
import com.wyp168.serviceedu.entity.chapter.VideoVo;
import com.wyp168.serviceedu.entity.vo.CourseInfoVo;
import com.wyp168.serviceedu.mapper.EduChapterMapper;
import com.wyp168.serviceedu.mapper.EduVideoMapper;
import com.wyp168.serviceedu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author wyp168
 * @since 2022-05-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Resource
    private EduVideoMapper videoMapper;

    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //1. 根据课程id查询课程里面的所有章节数
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
        //2. 根据课程id查询课程里面的所有小结数
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = videoMapper.selectList(wrapperVideo);
        //3.创建List集合，用于封装最后的结果
        List<ChapterVo> finalList = new ArrayList<>();
        //4.遍历查询章节List集合
        for (int i = 0; i < eduChapterList.size(); i++) {
            //每个章节
            EduChapter eduChapter = eduChapterList.get(i);
            //eduChapter对象中的值放到chapterVo中
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            //创建一个集合，用于封装相同章节的小结内容
            List<VideoVo> videoVoList = new ArrayList<>();
            //5.遍历小结集合
            for (int j = 0; j < eduVideoList.size(); j++) {
                if (eduVideoList.get(j).getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideoList.get(j),videoVo);
                    videoVoList.add(videoVo);
                }
            }
            //把封装后的小结集合存放到章节对象中
            chapterVo.setChildren(videoVoList);
            //把chapterVo 放到最终需要返回的集合中
            finalList.add(chapterVo);
        }

        return finalList;
    }


    //删除章节方法
    @Override
    public boolean deleteChapterById(String chapterId) {
        //查询小结
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        Integer count = videoMapper.selectCount(queryWrapper);
        //判断章节中是否有小结
        //如果有小结，则不进行删除，否则进行删除
        if (count > 0) {
            throw new GuliException(20001,"章节中有小结数据，不进行删除");
        } else {
            //删除数据
            int result = baseMapper.deleteById(chapterId);
            return result>0;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
